package cn.slipi.core.plugin;

import cn.slipi.common.exception.InternalException;
import cn.slipi.core.cache.ApiManager;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;

@AllArgsConstructor
public abstract class AbstractPlugin implements IPlugin {
    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractPlugin.class);

    //    private static final String[] plugins = new String[]{"http", "sign", "request", "response", "firewall", "gray_release", "rate_limiter"};
    private static final String[] plugins = new String[]{"http", "sign", "request", "response", "firewall", "gray_release", "rate_limiter"};

    @Autowired
    private final ApiManager apiManager;

    protected abstract Mono<Void> doExecute(ServerWebExchange exchange, IPluginChain chain);

    @Override
    public Mono<Void> execute(final ServerWebExchange exchange, final IPluginChain chain) {
        if (Arrays.asList(plugins).contains(getName())) {
            return doExecute(exchange, chain);
        } else {
            return chain.execute(exchange);
        }
    }

    public Mono<Void> response(HttpStatus httpStatus) {
        throw new InternalException(httpStatus.value(), httpStatus.getReasonPhrase());
    }
}
